﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ShakeIt_.ServiceReference2;

namespace ShakeIt_
{   
    public partial class MainPage : PhoneApplicationPage
    {

        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            Data data = new Data();
            // 将 listbox 控件的数据上下文设置为示例数据
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // 为 ViewModel 项加载数据
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
            if (Data.userId > 0)
            {
                ErrorMessageBox.Text = "登陆成功";
                username_text.Text = Data.username;
                AfterLog.Visibility = Visibility.Visible;
                BeforeLog.Visibility = Visibility.Collapsed;
            }
            SearchInput.Text = Data.searchInput;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/createPage.xaml", UriKind.Relative));
        }

        public bool isFocused = false;

        private void SearchInput_GotFocus(object sender, RoutedEventArgs e)
        {
            if (isFocused == false)
            {
                SearchInput.Text = "";
                isFocused = true;
            }
        }

        private void RegButton_Click(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/registerPage.xaml", UriKind.Relative));
        }

        private void LogButton_Click(object sender, RoutedEventArgs e)
        {
            Data.username = UsernameInput.Text;
            string password = PasswordInput.Password;
            Data.proxy.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(proxy_LoginCompleted);
            Data.proxy.LoginAsync(Data.username, password);
        }

        private void proxy_LoginCompleted(object sender, LoginCompletedEventArgs e)
        {
            if(e.Result==-1)
            {
                ErrorMessageBox.Text = "用户名和密码不匹配";
            }
            else if (e.Result >= 0)
            {
                Data.userId = e.Result;
                ErrorMessageBox.Text = "登陆成功";
                username_text.Text = Data.username;
                AfterLog.Visibility = Visibility.Visible;
                BeforeLog.Visibility = Visibility.Collapsed;
                Data.proxy.GetAllCommentCompleted += new EventHandler<GetAllCommentCompletedEventArgs>(proxy_GetAllCommentCompleted);
                Data.proxy.GetAllCommentAsync(Data.userId);
            }
        }

        private void proxy_GetAllCommentCompleted(object sender, GetAllCommentCompletedEventArgs e)
        {
            Data.calculateScore(Data.username);
            level_text.Text = "评分: " + Data.myScore;
        }

        private void DisplayInMap_Click(object sender, RoutedEventArgs e)
        {
            FindActivity();
            this.NavigationService.Navigate(new Uri("/nearsearchPage.xaml", UriKind.Relative));
        }

        private void DisplayInList_Click(object sender, RoutedEventArgs e)
        {
            FindActivity();
            this.NavigationService.Navigate(new Uri("/researchresultPage.xaml", UriKind.Relative));
        }

        private void FindActivity()
        {
            if (SearchInput.Text.Equals("输入你想查询的活动关键词"))
            {
                SearchInput.Text = "";
            }
            Data.searchInput = SearchInput.Text;
            Data.FindActivity();
        }

        private void info_button_Click(object sender, RoutedEventArgs e)
        {
            Data.proxy.FindUserAsync(Data.userId);
            this.NavigationService.Navigate(new Uri("/infoPage.xaml", UriKind.Relative));
        }

        private void fans_button_Click(object sender, EventArgs e)
        {
            Data.proxy.GetFansAsync(Data.userId);
            this.NavigationService.Navigate(new Uri("/fansPage.xaml", UriKind.Relative));
        }

        private void favour_button_Click(object sender, EventArgs e)
        {
            Data.proxy.GetFocusAsync(Data.userId);
            this.NavigationService.Navigate(new Uri("/favourPage.xaml", UriKind.Relative));
        }

        private void message_button_Click(object sender, RoutedEventArgs e)
        {
            Data.proxy.FindChatByReceiveIdAsync(Data.userId);
            this.NavigationService.Navigate(new Uri("/myMessagePage.xaml", UriKind.Relative));
        }

        private void comment_button_Click(object sender, RoutedEventArgs e)
        {
            Data.userFocusedId = Data.userId;
            Data.userFocusedName = Data.username;
            this.NavigationService.Navigate(new Uri("/commentPage.xaml", UriKind.Relative));
        }
    }
}